package com.echat.messagesender.sender.sms.bean;

import com.echat.messagesender.sender.email.bean.enumclass.EmailServerName;
import com.echat.messagesender.sender.exception.EchatSmsException;
import com.echat.messagesender.sender.sms.bean.enumclass.SmsServerName;
import com.echat.messagesender.sender.util.JsonUtil;
import lombok.Getter;

import java.util.Map;

/**
 * Created by chenmohan on 2018/08/16
 *
 * @description
 */
@Getter
public class SmsConfig {

    private AliSmsConfig aliSmsConfig;
    private Echat253Config echat253Config;
    private SmsWayConfig smsWayConfig;
    private SmsServerName serverType;

    public SmsConfig(Long platformId, String platformName, Long platformAccountId, Map<String, String> configMap) {
        if (platformAccountId == null) {
            throw new EchatSmsException("必须配置平台服务id！");
        }

        serverType = SmsServerName.get(platformId);
        switch (serverType) {
            case AliCloud:
                this.aliSmsConfig = (AliSmsConfig) JsonUtil.toBean(configMap, AliSmsConfig.class);
                this.aliSmsConfig.platformAccountId = platformAccountId;
                this.aliSmsConfig.platformName = platformName;
                aliSmsConfig.check();
                return;
            case Echat253:
                this.echat253Config = (Echat253Config) JsonUtil.toBean(configMap, Echat253Config.class);
                this.echat253Config.platformAccountId = platformAccountId;
                this.echat253Config.platformName = platformName;
                echat253Config.check();
                return;
            case SmsWay:
                this.smsWayConfig = (SmsWayConfig) JsonUtil.toBean(configMap, SmsWayConfig.class);
                this.smsWayConfig.platformAccountId = platformAccountId;
                this.smsWayConfig.platformName = platformName;
                smsWayConfig.check();
                return;
            default:
                throw new EchatSmsException("无效的SmsServerType类型，请检查SmsConfig类的SmsServerType");
        }
    }

    public Object get() {
        switch (this.serverType) {
            case AliCloud:
                return this.aliSmsConfig;
            case Echat253:
                return this.echat253Config;
            case SmsWay:
                return this.smsWayConfig;
        }
        return null;
    }

}
